//
//  PartnerDetailsController.m
//  JiFengDaRen
//
//  Created by shouhan on 2018/6/8.
//  Copyright © 2018年 陈永发. All rights reserved.
//

#import "PartnerDetailsController.h"
#import "PartnerTableViewCell.h"
#import "PartnerModel.h"

@interface PartnerDetailsController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger pageindex;
}
@property (nonatomic, strong) UITableView * detailsTableView;

@property (nonatomic, strong) NSMutableArray * dataSource;

@property (nonatomic, strong) UILabel * numLabel;

@end

@implementation PartnerDetailsController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    pageindex = 1;
    self.dataSource = [NSMutableArray array];
    
    self.title = [Tool numberSuitScanf:self.phone];
    
    self.numLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, __I6Height(40))];
    self.numLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    self.numLabel.font = KSHSystemFont(14);
    self.numLabel.backgroundColor = [UIColor colorWithHexString:@"#F7F8FB"];
    self.numLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.numLabel];
    
    [self detailsTableView];
    
    [self DoRequestGet_sub_register:YES];
    
    self.detailsTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    
    self.detailsTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PartnerTableViewCell * cell = [PartnerTableViewCell tableViewCellWithTableView:tableView];
    cell.arrow.hidden = YES;
    cell.model = self.dataSource[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return __I6Height(50);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PartnerModel *model = self.dataSource[indexPath.row];
    PartnerDetailsController * VC = [[PartnerDetailsController alloc] init];
    VC.phone = model.phone;
    [self.navigationController pushViewController:VC animated:YES];
}

// 懒加载
- (UITableView *)detailsTableView{
    if (!_detailsTableView) {
        _detailsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, __I6Height(40), KScreenWidth, KScreenHeight-__I6Height(40)-KNAVIGATIONBARHEIGHT) style:UITableViewStylePlain];
        _detailsTableView.delegate = self;
        _detailsTableView.dataSource = self;
        _detailsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _detailsTableView.backgroundColor = [UIColor colorWithHexString:@"#F7F8FB"];
        
        [self.view addSubview:_detailsTableView];
    }
    return _detailsTableView;
}
-(void)refresh
{
    pageindex = 1;
    [self DoRequestGet_sub_register:YES];
}
-(void)loadMore
{
    pageindex ++;
    [self DoRequestGet_sub_register:NO];
}

- (void)DoRequestGet_sub_register:(BOOL)isRemove{
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setValue:[NSString stringWithFormat:@"%ld",(long)pageindex] forKey:@"pageindex"];
    [dict setValue:@"10" forKey:@"count"];
    [dict setValue:self.phone forKey:@"cooperator"];
    [PPNetworkHelper POST:Get_sub_register parameters:dict cache:NO encrypt:YES decode:YES responseCache:^(id responseCache) {
        
    } success:^(id result) {
        
        if ([result[@"success"] boolValue] == YES) {
            if (isRemove) {
                [self.dataSource removeAllObjects];
            }
            [self.dataSource addObjectsFromArray:[PartnerModel mj_objectArrayWithKeyValuesArray:result[@"data"]]];
            self.numLabel.text = [NSString stringWithFormat:@"总共%lu人",(unsigned long)self.dataSource.count];
        }
        [self.detailsTableView.mj_header endRefreshing];
        [self.detailsTableView.mj_footer endRefreshing];
        [self.detailsTableView reloadData];
        
    } failure:^(NSError *error) {
        [self.detailsTableView.mj_header endRefreshing];
        [self.detailsTableView.mj_footer endRefreshing];
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
